package vip.web3.api.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import vip.web3.api.entity.biz.UserEntity;
import vip.web3.api.entity.biz.UserWalletEntity;
import vip.web3.api.form.user.RegisterForm;
import vip.web3.api.vo.biz.UserInfoVo;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * aut-horlilei
 */
@Mapper
public interface UserMapper {
    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    /**
     * toUserEntity
     *
     * @param form form
     * @return UserFront
     */
    UserEntity toUserEntity(RegisterForm form);

    /**
     * toUserInfoVo
     *
     * @param userEntity       userEntity
     * @param userWalletEntity userWalletEntity
     * @return UserInfoVo
     */
    @Mapping(source = "userEntity.createTime", target = "registerTime")
    @Mapping(target = "amountCny", ignore = true)
    @Mapping(target = "amountUsdt", ignore = true)
    @Mapping(target = "withdrawCny", ignore = true)
    @Mapping(target = "withdrawUsdt", ignore = true)
    UserInfoVo toUserInfoVo(UserEntity userEntity, UserWalletEntity userWalletEntity);

    /**
     * mergeUserInfoVo
     *
     * @param userEntity       userEntity
     * @param userWalletEntity userWalletEntity
     * @return UserInfoVo
     */
    default UserInfoVo mergeUserInfoVo(UserEntity userEntity, UserWalletEntity userWalletEntity, String url) {
        BigDecimal amountCny = Objects.isNull(userWalletEntity.getAmountCny()) ? BigDecimal.ZERO : userWalletEntity.getAmountCny();
        BigDecimal amountUsdt = Objects.isNull(userWalletEntity.getAmountUsdt()) ? BigDecimal.ZERO : userWalletEntity.getAmountUsdt();
        BigDecimal withdrawCny = Objects.isNull(userWalletEntity.getWithdrawCny()) ? BigDecimal.ZERO : userWalletEntity.getWithdrawCny();
        BigDecimal withdrawUsdt = Objects.isNull(userWalletEntity.getWithdrawUsdt()) ? BigDecimal.ZERO : userWalletEntity.getWithdrawUsdt();

        UserInfoVo userInfoVo = this.toUserInfoVo(userEntity, userWalletEntity);
        userInfoVo.setInviteUrl(url + userInfoVo.getInviteCode());
        userInfoVo.setAmountCny(amountCny.stripTrailingZeros().toPlainString());
        userInfoVo.setAmountUsdt(amountUsdt.stripTrailingZeros().toPlainString());
        userInfoVo.setWithdrawCny(withdrawCny.stripTrailingZeros().toPlainString());
        userInfoVo.setWithdrawUsdt(withdrawUsdt.stripTrailingZeros().toPlainString());
        return userInfoVo;
    }
}
